package com.sanjiahantang.youjiaclient.module.store.preseneters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseApplication;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.StaticValue;
import com.sanjiahantang.youjiaclient.module.cases.views.impl.DesignerDetailActivity;
import com.sanjiahantang.youjiaclient.module.me.require.beans.BidDesignerBean;
import com.sanjiahantang.youjiaclient.module.store.models.IStoreModel;
import com.sanjiahantang.youjiaclient.module.store.models.impl.StoreModel;
import com.sanjiahantang.youjiaclient.module.store.views.StoreDesignerView;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yan on 2018/1/5.
 */

public class StoreDesignerPresenter extends BaseMvpPresenter<StoreDesignerView> {
    private IStoreModel storeModel;

    private QuickAdapter adapter;

    public StoreDesignerPresenter() {
        this.storeModel = new StoreModel();
    }

    /**
     * 请求设计师列表数据
     *
     * @param context
     * @param id
     * @param listener
     */
    public void loadDataDesignerList(Context context, String id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        if (!TextUtils.isEmpty(ConfigValue.DATA_KEY)) {
            param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        }
        param.put(KEY_ROLEID, id);

        mView.showProgress();
        storeModel.loadStoreDesignerList(context, param, new CallBack<BidDesignerBean>() {
            @Override
            public void onSuccess(BidDesignerBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    adapter.clear();
                    adapter.addAll(response.getData());
                    listener.onLoad(response.getData());
//
                } else if (response.getCode() == StaticValue.RESPONSE_CODE_FAIL) {
                    ((Activity) context).finish();
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    /**
     * listview初始化
     *
     * @param context
     */
    public void initListView(Activity context, StoreDesignerView.InitDataListener listener) {
        adapter = new QuickAdapter<BidDesignerBean.Data>(context, R.layout.item_designer) {
            @Override
            protected void convert(BaseAdapterHelper helper, final BidDesignerBean.Data item) {
                helper.setText(R.id.name, item.getUserName());
                helper.setText(R.id.txtRate, item.getSysPoints() + "评分");
                helper.setText(R.id.time, "签约 " + String.valueOf((item.getToDay() / 365) + 1) + "年");
                helper.setText(R.id.txtCity, item.getAddress());
                ImageView iv_image = helper.getView(R.id.iv_image);
                Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_head_default).into(iv_image);
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(item.getRoleId())) {
                            context.startActivity(new Intent(context, DesignerDetailActivity.class)
                                    .putExtra(KEY_PHONE, item.getRoleId())
                            );
                            BaseApplication.addActivity((Activity) context);
                        }
                    }
                });
            }
        };
        listener.initListViewAdapter(adapter);
    }

    private void hideProgress() {
        if (mView != null) {
            mView.hideProgress();
        }
    }
}
